package com.shop.shoppingmall.util.oss;


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class XMLUtil {
	/**
	 * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。
	 * @param strxml
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public static Map doXMLParse(String strxml) throws JDOMException, IOException {
		strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

		if(null == strxml || "".equals(strxml)) {
			return null;
		}

		Map m = new HashMap();

		InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(in);
		Element root = doc.getRootElement();
		List list = root.getChildren();
		Iterator it = list.iterator();
		while(it.hasNext()) {
			Element e = (Element) it.next();
			String k = e.getName();
			String v = "";
			List children = e.getChildren();
			if(children.isEmpty()) {
				v = e.getTextNormalize();
			} else {
				v = XMLUtil.getChildrenText(children);
			}

			m.put(k, v);
		}

		//关闭流
		in.close();

		return m;
	}

	/**
	 * 获取子结点的xml
	 * @param children
	 * @return String
	 */
	public static String getChildrenText(List children) {
		StringBuffer sb = new StringBuffer();
		if(!children.isEmpty()) {
			Iterator it = children.iterator();
			while(it.hasNext()) {
				Element e = (Element) it.next();
				String name = e.getName();
				String value = e.getTextNormalize();
				List list = e.getChildren();
				sb.append("<" + name + ">");
				if(!list.isEmpty()) {
					sb.append(XMLUtil.getChildrenText(list));
				}
				sb.append(value);
				sb.append("</" + name + ">");
			}
		}

		return sb.toString();
	}

	public static UnifiedOrderResult getUnifiedOrderResult(String xml){
		UnifiedOrderResult unifieOrderResult = new UnifiedOrderResult();
		try {
			StringReader read = new StringReader(xml);
			// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
			InputSource source = new InputSource(read);
			// 创建一个新的SAXBuilder
			SAXBuilder sb = new SAXBuilder();
			// 通过输入源构造一个Document
			Document doc;
			doc = (Document) sb.build(source);

			Element root = doc.getRootElement();// 指向根节点
			List<Element> list = root.getChildren();

			if(list!=null&&list.size()>0){
				for (Element element : list) {
					System.out.println("key是："+element.getName()+"，值是："+element.getText());

					if("return_code".equals(element.getName())){
						unifieOrderResult.setReturn_code(element.getText());
					}

					if("return_msg".equals(element.getName())){
						unifieOrderResult.setReturn_msg(element.getText());
					}

					if("appid".equals(element.getName())){
						unifieOrderResult.setAppid(element.getText());
					}


					if("mch_id".equals(element.getName())){
						unifieOrderResult.setMch_id(element.getText());
					}

					if("nonce_str".equals(element.getName())){
						unifieOrderResult.setNonce_str(element.getText());
					}

					if("sign".equals(element.getName())){
						unifieOrderResult.setSign(element.getText());
					}

					if("result_code".equals(element.getName())){
						unifieOrderResult.setResult_code(element.getText());
					}

					if("prepay_id".equals(element.getName())){
						unifieOrderResult.setPrepay_id(element.getText());
					}

					if("trade_type".equals(element.getName())){
						unifieOrderResult.setTrade_type(element.getText());
					}
					if("code_url".equals(element.getName())){
						unifieOrderResult.setCode_url(element.getText());
					}
				}
			}
//			String code_url = unifieorderResult.getCode_url();
			String prepay_id = unifieOrderResult.getPrepay_id();
//			System.out.println(code_url);
			System.out.println(prepay_id);
//			QRCodeUtil.QRCode(productId,code_url, request, response);
		} catch (JDOMException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}

		return unifieOrderResult;
	}
	public static String setXML(String return_code, String return_msg) {
		return "<xml><return_code><![CDATA[" + return_code
				+ "]]></return_code><return_msg><![CDATA[" + return_msg
				+ "]]></return_msg></xml>";
	}
}
